home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / GDIDIB.PAK / PALETTE.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  8KB  |  299 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993 - 1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE:   palette.c
  9. //
  10. //  PURPOSE:  Contains palette routines
  11. //
  12. //  FUNCTIONS:
  13. //    ProcessPaletteChanged  - Process the WM_PALETTECHANGED message
  14. //    ProcessQueryNewPalette - Process the WM_QUERYNEWPALETTE message
  15. //    IsPaletteDevice        - Checks display for Windows palette
  16. //                             capabilities.
  17. //    PalEntriesOnDevice     - Get the number of palette entries on
  18. //                             the specified device
  19. //    GetSystemPalette       - Gets the current system palette
  20. //    ColorsInPalette        - Given a handle to a palette, returns the
  21. //                             # of colors in that palette.
  22. //
  23. //  COMMENTS:
  24. //
  25.  
  26.  
  27. #include <windows.h>            // required for all Windows applications
  28. #include <windowsx.h> 
  29. #include "globals.h"            // prototypes specific to this application
  30. #include "palette.h" 
  31.  
  32.  
  33. //
  34. //  FUNCTION: ProcessPaletteChanged(HWND, WPARAM)
  35. //
  36. //  PURPOSE:  Process the WM_PALETTECHANGED message
  37. //
  38. //  PARAMETERS:
  39. //    hwnd     -  Handle of window receiving the message.
  40. //    wparam    - Handle of window that changed the palette.
  41. //
  42. //  RETURN VALUE:
  43. //    TRUE if message processed, else FALSE.
  44. //
  45. //  COMMENTS:
  46. //    The WM_PALETTECHANGED message informs all windows that the window with
  47. //    input focus has realized its logical palette, thereby changing the
  48. //    system palette. This message allows a window without input focus that
  49. //    uses a color palette to realize its logical palettes and update its
  50. //    client area.
  51. //
  52. //    This message is sent to all windows, including the one that changed
  53. //    the system palette and caused this message to be sent. The wParam of
  54. //    this message contains the handle of the window that caused the system
  55. //    palette to change. To avoid an infinite loop, care must be taken to
  56. //    check that the wParam of this message does not match the window's handle.
  57. //            
  58. LRESULT ProcessPaletteChanged(HWND hwnd, WPARAM wparam)
  59. {
  60.     HDC      hDC;      // Handle to device context
  61.     HPALETTE hOldPal;  // Handle to previous logical palette
  62.  
  63.     // Before processing this message, make sure we are indeed using a palette
  64.     if (hPalette)
  65.     {
  66.         // If this application did not change the palette, select
  67.         // and realize this application's palette
  68.         if ((HWND)wparam != hwnd)
  69.         {
  70.             // get a display context for the window
  71.             hDC = GetDC(hwnd);
  72.  
  73.             // Select and realize our palette
  74.             hOldPal = SelectPalette(hDC, hPalette, TRUE);
  75.             RealizePalette(hDC);
  76.  
  77.             // When updating the colors for an inactive window,
  78.             // UpdateColors can be called because it is faster than
  79.             // redrawing the client area (even though the results are
  80.             // not as good)
  81.             UpdateColors(hDC);
  82.  
  83.             // Clean up
  84.             if (hOldPal)
  85.                 SelectPalette(hDC, hOldPal, FALSE);   
  86.                 
  87.             ReleaseDC(hwnd, hDC);
  88.         } 
  89.             
  90.         // message processed
  91.         return TRUE;  
  92.     }
  93.          
  94.     // message not processed
  95.     return FALSE;  
  96. }
  97.  
  98.  
  99. //
  100. //  FUNCTION: ProcessQueryNewPalette(HWND)
  101. //
  102. //  PURPOSE:  Process the WM_QUERYNEWPALETTE message.
  103. //
  104. //  PARAMETERS:
  105. //    hwnd     -  Handle of window receiving the message. 
  106. //
  107. //  RETURN VALUE:
  108. //    TRUE if message processed, else FALSE.
  109. //
  110. //  COMMENTS:
  111. //    The WM_QUERYNEWPALETTE message informs a window that it is about to
  112. //    receive input focus. In response, the window receiving focus should
  113. //    realize its palette as a foreground palette and update its client
  114. //    area. If the window realizes its palette, it should return TRUE;
  115. //    otherwise, it should return FALSE.
  116. //
  117. LRESULT ProcessQueryNewPalette(HWND hwnd)
  118. {
  119.     HDC      hDC;      // Handle to device context
  120.     HPALETTE hOldPal;  // Handle to previous logical palette
  121.  
  122.     // Only process the message if a palette is in use
  123.     if (hPalette)
  124.     {
  125.         // get a display context for the window       
  126.         hDC = GetDC(hwnd);
  127.  
  128.         // Select and realize our palette
  129.         hOldPal = SelectPalette(hDC, hPalette, FALSE);
  130.         RealizePalette(hDC);
  131.  
  132.         // Redraw the entire client area
  133.         InvalidateRect(hwnd, NULL, TRUE);
  134.         UpdateWindow(hwnd);
  135.  
  136.         // Clean up
  137.         if (hOldPal)
  138.             SelectPalette(hDC, hOldPal, FALSE);
  139.         ReleaseDC(hwnd, hDC);
  140.  
  141.         // Message processed
  142.         return TRUE;
  143.     }
  144.     
  145.     // Message not processed
  146.     return FALSE;
  147. }
  148.  
  149. //
  150. //  FUNCTION: IsPaletteDevice()
  151. //
  152. //  PURPOSE:  Checks display for Windows palette capabilities.
  153. //
  154. //  PARAMETERS:
  155. //    None.
  156. //
  157. //  RETURN VALUE:
  158. //    TRUE if a palette device, FALSE otherwise
  159. //
  160. //  COMMENTS:
  161. //
  162. BOOL IsPaletteDevice()
  163. {
  164.     HDC hdc;
  165.     int iRastercaps;
  166.     
  167.     hdc = GetDC(NULL);
  168.     iRastercaps = GetDeviceCaps(hdc, RASTERCAPS);
  169.     ReleaseDC(NULL, hdc);
  170.     
  171.     return ((iRastercaps & RC_PALETTE) ? TRUE : FALSE);
  172. }
  173.  
  174.  
  175. //
  176. //  FUNCTION: PalEntriesOnDevice(HDC)
  177. //
  178. //  PURPOSE:  Get the number of palette entries on the specified device
  179. //
  180. //  PARAMETERS:
  181. //    hDC          - device context
  182. //
  183. //  RETURN VALUE:
  184. //    The number of palette entries on device
  185. //
  186. //  COMMENTS:
  187. //
  188. //
  189. int PalEntriesOnDevice(HDC hDC)
  190. {
  191.     int nColors;  // number of colors
  192.  
  193.     // Find out the number of palette entries on this device.
  194.     
  195.     nColors = GetDeviceCaps(hDC, SIZEPALETTE);
  196.  
  197.     // For non-palette devices, we'll use the # of system
  198.     // colors for our palette size.
  199.  
  200.     if (!nColors)
  201.         nColors = GetDeviceCaps(hDC, NUMCOLORS);
  202.  
  203.     return nColors;
  204. }
  205.  
  206.  
  207. //
  208. //  FUNCTION: GetSystemPalette(void)
  209. //
  210. //  PURPOSE:  This routine returns a handle to a palette which represents
  211. //            the system palette (each entry is an offset into the system
  212. //            palette instead of an RGB with a flag of PC_EXPLICIT).
  213. //            Useful for dumping the system palette.
  214. //
  215. //  PARAMETERS:
  216. //    None.
  217. //
  218. //  RETURN VALUE:
  219. //    Handle to a palette consisting of the system palette.
  220. //
  221. //  COMMENTS:
  222. //
  223.  
  224. HPALETTE GetSystemPalette(void)
  225. {
  226.     HDC          hDC;
  227.     HPALETTE     hPal;
  228.     HANDLE       hLogPal;
  229.     LPLOGPALETTE lpLogPal;
  230.      int          i, nColors;
  231.  
  232.      // Find out how many palette entries we want.
  233.  
  234.      hDC = GetDC(NULL);
  235.      if (!hDC)
  236.           return NULL;
  237.  
  238.      nColors = PalEntriesOnDevice(hDC);
  239.      ReleaseDC(NULL, hDC);
  240.  
  241.      // Allocate room for the palette and lock it.
  242.  
  243.      hLogPal = GlobalAlloc(GHND, sizeof(LOGPALETTE) +
  244.                                   nColors * sizeof(PALETTEENTRY));
  245.  
  246.      if (!hLogPal)
  247.           return NULL;
  248.  
  249.      lpLogPal = (LPLOGPALETTE)GlobalLock(hLogPal);
  250.  
  251.      lpLogPal->palVersion    = 0x300;
  252.      lpLogPal->palNumEntries = (WORD) nColors;
  253.  
  254.      for (i = 0;  i < nColors;  i++)
  255.      {
  256.           lpLogPal->palPalEntry[i].peBlue = (BYTE) 0;
  257.           *((LPWORD)(&lpLogPal->palPalEntry[i].peRed)) = (BYTE) i;
  258.           lpLogPal->palPalEntry[i].peFlags = PC_EXPLICIT;
  259.     }
  260.  
  261.     // Go ahead and create the palette.  Once it's created,
  262.      //  we no longer need the LOGPALETTE, so free it.
  263.  
  264.     hPal = CreatePalette(lpLogPal);
  265.  
  266.     GlobalUnlock(hLogPal);
  267.     GlobalFree(hLogPal);
  268.  
  269.     return hPal;
  270. }
  271.  
  272.  
  273. //
  274. //  FUNCTION: ColorsInPalette(HPALETTE)
  275. //
  276. //  PURPOSE:  Given a handle to a palette, returns the # of colors
  277. //            in that palette.
  278. //
  279. //  PARAMETERS:
  280. //    hPal      - Handle to palette we want info on.
  281. //
  282. //  RETURN VALUE:
  283. //    Number of colors in the given palette
  284. //
  285. //  COMMENTS:
  286. //
  287.  
  288. int ColorsInPalette(HPALETTE hPal)
  289. {
  290.     int nColors=0;
  291.  
  292.     if (!hPal)
  293.         return 0;
  294.  
  295.     GetObject(hPal, sizeof(nColors), (LPSTR)&nColors);
  296.  
  297.     return nColors;
  298. }
  299.